import { getTableData } from "../../utils/tableData"

// 默认数据
let initialBillDatax = JSON.parse(getTableData()) || {}

const BillDatax = (state = initialBillDatax, action) => {
  // 获取信息
  if (action.type === 'setBillData') {
    return action.payload
  }
  // 添加信息
  if (action.type === 'addBillData') {
    return [
      action.payload,
      ...state
    ]
  }
  // 修改信息
  if (action.type === 'changeBillData') {
    return state.map(item => {
      if (item.key === action.payload.key) {
        return action.payload
      } else {
        return item
      }
    })
  }
  // 筛选信息
  if (action.type === 'getBillData') {
    state = action.payload[1]
    return state.filter(item => {
      if (action.payload[0][0] && action.payload[0][1]) {
        if ((item.date.substr(0, 4) + '-' + item.date.substr(5, 2)) === action.payload[0][0] && item.sort === action.payload[0][1]) {
          return item
        }
      } else if (action.payload[0][0] === '' && action.payload[0][1] === undefined) {
        return state
      } else {
        if ((item.date.substr(0, 4) + '-' + item.date.substr(5, 2)) === action.payload[0][0]) {
          return item
        }
        // 查看筛选的信息
        // console.log(action.payload[0][1], item.sort)
        if (item.sort === action.payload[0][1]) {
          return item
        }
      }
    })
  }
  return state
}
export default BillDatax